package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.yw.IYwFundApplyService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.system.request.CustomerRestRequest;
import com.zjdiepu.www.model.yw.dto.YwFundApply;
import com.zjdiepu.www.model.yw.ext.YwFundApplyExt;
import com.zjdiepu.www.model.yw.request.YwFundApplyRequest;
import com.zjdiepu.www.util.HtmlUtil;

/**
 * 申请垫资Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwFundApplyController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"申请垫资"}, produces="application/json;charset=UTF-8")
public class YwFundApplyController extends BaseController {
    @Resource
    private IYwFundApplyService ywFundApplyService;
    @Resource
    private TokenUserHelper tokenUserHelper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: queryApplyFund  <br/>
     * @Description: 查询申请垫资 <br/>
     * @param customerRestRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "查询申请垫资", response = YwFundApplyRequest.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("queryApplyFund")
    public ApiResponse queryApplyFund(@Validated @RequestBody CustomerRestRequest customerRestRequest) {
    	tokenUserHelper.getUser();
    	YwFundApplyExt ywFundApplyExt = new YwFundApplyExt();
    	ywFundApplyExt.setBaseWhere();
    	ywFundApplyExt.setRows(1);
    	ywFundApplyExt.setBusiId(customerRestRequest.getBusiId());
    	List<YwFundApply> ywFundApplys = ywFundApplyService.selectByExt(ywFundApplyExt);
    	if(CollectionUtils.isNotEmpty(ywFundApplys)) {
    		YwFundApply ywFundApply = ywFundApplys.get(0);
    		YwFundApplyRequest ywFundApplyRequest = new YwFundApplyRequest();
    		ywFundApplyRequest.setId(ywFundApply.getId());
    		ywFundApplyRequest.setRemarks(HtmlUtil.filterHtml(ywFundApply.getRemarks()));
    		ywFundApplyRequest.setCusId(ywFundApply.getCusId());
    		ywFundApplyRequest.setBusiId(ywFundApply.getBusiId());
    		ywFundApplyRequest.setFeesId(ywFundApply.getFeesId());
    		ywFundApplyRequest.setDateFist(ywFundApply.getDateFist());
    		return ApiResponse.success(ywFundApply);
    	} else {
    		return ApiResponse.success(null);
    	}
         
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: applyFund  <br/>
     * @Description: 申请垫资 <br/>
     * @param ywFundApplyRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请垫资", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("applyFund")
    public ApiResponse applyFund(@Validated(Add.class) @RequestBody YwFundApplyRequest ywFundApplyRequest, HttpServletRequest request) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	YwFundApply ywFundApply = new YwFundApply();
    	ywFundApply.setId(ywFundApplyRequest.getId());
		ywFundApply.setBusiId(ywFundApplyRequest.getBusiId());
		ywFundApply.setFeesId(ywFundApplyRequest.getFeesId());
		ywFundApply.setCusId(ywFundApplyRequest.getCusId());
		ywFundApply.setDateFist(ywFundApplyRequest.getDateFist());
		ywFundApply.setRemarks(ywFundApplyRequest.getRemarks());
		ServletContext  application  = request.getSession(true).getServletContext();  
		String serverRealPath = application.getRealPath("/WEB-INF/configure/fkshConfV001.json");
    	return ywFundApplyService.saveApplyFund(ywFundApply, currentUser, serverRealPath);
         
    }

}
